package com.glory.dgcpcpda.utils;

import android.util.Log;

import com.glory.dgcpcpda.activity.technicalservice.Eqp_EqpKeepActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.callback.RequestCallBack;
import com.glory.dgcpcpda.entity.EqpKeepBean;
import com.glory.dgcpcpda.entity.request.BaseRequest;
import com.glory.dgcpcpda.entity.request.HeaderBean;
import com.glory.dgcpcpda.entity.request.RequestBean;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lzy.okgo.BuildConfig;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_NetUtils;

import org.apache.commons.lang3.StringEscapeUtils;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author yi.zhang
 * @date 2019/6/10
 * @desc 网络请求类
 */
public class NetUtils {

    public static <T> void requestPostNet(String messageName, String method,Object tag, JsonObject jsonString,FreeUI_EntityCallBack<T> callBack){
        RequestBean requestBean = new RequestBean();
        requestBean.setHeader(new HeaderBean(messageName));
        requestBean.setBody(jsonString);
        BaseRequest request = new BaseRequest();
        request.setRequest(requestBean);
        Map<String,String> map = new HashMap<>();
        map.put("senderId","EAP2CPCSender");
        map.put("requestMessage",StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        map.put("timeOut","50000");
        Log.e("requestData", "requestData: " + StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        FreeApi_LogUtils.saveErrorMessages(map.toString(),"请求json");
        FreeApi_LogUtils.saveErrorMessages(Constant.REQUEST_URL_DEBUG + "/api/" + method,"请求地址");
        FreeApi_NetUtils.requestPostNet( Constant.REQUEST_URL_DEBUG + "/api/" + method, tag, map, callBack);
    }

    public static <T> void mRequestPostNet(String messageName,String name, String method,Object tag, JsonObject jsonString,FreeUI_EntityCallBack<T> callBack){
        RequestBean requestBean = new RequestBean();
        requestBean.setHeader(new HeaderBean(messageName,name));
        requestBean.setBody(jsonString);
        BaseRequest request = new BaseRequest();
        request.setRequest(requestBean);
        Map<String,String> map = new HashMap<>();
        map.put("senderId","EAP2CPCSender");
        map.put("requestMessage",StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        map.put("timeOut","50000");
        Log.e("requestData", "requestData: " + StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        FreeApi_LogUtils.saveErrorMessages(map.toString(),"请求json");
        FreeApi_NetUtils.requestPostNet( Constant.REQUEST_URL_DEBUG + "/api/" + method, tag, map, callBack);
    }

    public static <T> void requestPostCPC(String messageName, String method,Object tag, JsonObject jsonString,FreeUI_EntityCallBack<T> callBack){
        requestPostCPCUrl(false, messageName, method, tag, jsonString, callBack);
    }

    // 搞个固定地址，不然后续都设错了，咋办？
    public static <T> void requestPostCPCUrl(Boolean isfixed,String messageName, String method,Object tag, JsonObject jsonString,FreeUI_EntityCallBack<T> callBack){
        RequestBean requestBean = new RequestBean();
        requestBean.setHeader(new HeaderBean(messageName));
        requestBean.setBody(jsonString);
        BaseRequest request = new BaseRequest();
        request.setRequest(requestBean);
        Map<String,String> map = new HashMap<>();
        map.put("requestMessage",StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        Log.e("requestData", "requestData: " + StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        FreeApi_LogUtils.saveErrorMessages(map.toString(),"请求json");
        FreeApi_NetUtils.requestPostNet( isfixed ? "http://192.168.1.72:8086/api/PDA/" + method : Constant.REQUEST_CPC + "/api/PDA/" + method, tag, map, callBack);
    }

    public static <T> void requestMESPostNet(String messageName,Object tag, JsonObject jsonString,FreeUI_EntityCallBack<T> callBack) {
        RequestBean requestBean = new RequestBean();
        requestBean.setHeader(new HeaderBean(messageName));
        requestBean.setBody(jsonString);
        BaseRequest request = new BaseRequest();
        request.setRequest(requestBean);
        Map<String, String> map = new HashMap<>();
        map.put("senderId", "EAP2CPCSender");
        map.put("requestMessage", StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        map.put("timeOut", "50000");
        Log.e("requestData", "requestData: " + StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        FreeApi_LogUtils.saveErrorMessages(map.toString(),"MES提交JSON");
        FreeApi_NetUtils.requestPostNet(Constant.REQUEST_MES + "/esbrest/RestService/postrequest", tag, map, callBack);
    }

    // 可传参数 可传文件
    public static <T> void requestCpcPpf(String url, Map<String, String> params, Map<String, File> files, RequestCallBack<T> callBack){
        url = Constant.REQUEST_CPC + "/api/PDA/" + url;

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

        if(params != null){
            for (String key : params.keySet()) {
                builder.addFormDataPart(key, params.get(key));
            }
        }

        if(files != null){
            for (String key : files.keySet()) {
                builder.addFormDataPart(key,files.get(key).getPath(),
                        RequestBody.create(MediaType.parse("application/octet-stream"),files.get(key)));
            }
        }

        RequestBody body = builder.build();

        Request request = new Request.Builder().url(url).method("POST", body).build();
        client.newCall(request).enqueue(callBack);
    }

    // 拍照上传图片
    public static <T> void requestCpcPhoto(String method, Map<String, String> params, List files, RequestCallBack<T> callBack){
        String url = Constant.REQUEST_URL_DEBUG + "/api/" + method;
        RequestBean requestBean = new RequestBean();
        requestBean.setBody(params);
        BaseRequest request = new BaseRequest();
        request.setRequest(requestBean);

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("requestMessage", StringEscapeUtils.unescapeJson(new Gson().toJson(request)));
        if(files != null){
            builder.addFormDataPart("filePath",files.get(0).toString(),
                    RequestBody.create(MediaType.parse("application/octet-stream"),new File(files.get(0).toString())));
        }
        RequestBody body = builder.build();
        Request requestHttp = new Request.Builder().url(url).method("POST", body).build();
        client.newCall(requestHttp).enqueue(callBack);
    }


}
